package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryByParentId(Long pid){
        Category record = new Category();
        record.setParentId(pid);
        List<Category> select = categoryMapper.select(record);
        if (CollectionUtils.isEmpty(select)){
            throw new LyException(ExceptionEnum.CATEGORY_NO_FOUND);
        }
        return BeanHelper.copyWithCollection(select,CategoryDTO.class);
    }


    public List<CategoryDTO> queryByBrandId(Long brandId) {
       List<Category> categories = this.categoryMapper.queryByBrandId(brandId);
          if (CollectionUtils.isEmpty(categories)){
              throw new LyException(ExceptionEnum.CATEGORY_NO_FOUND);
          }
          return   BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }


    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
           //新增一个接口，新增了一次性传入一个集合参数查询
        List<Category> categories = this.categoryMapper.selectByIdList(categoryIds);
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NO_FOUND);
        }
        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }
}
